package com.sq.partner.manager.controller;
import com.sq.partner.manager.service.AntVideoService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 蚂蚁影像查询
 * SQ
 */
@Controller
@RequestMapping("antVideoController")
public class AntVideoController {
    @Resource
    AntVideoService antVideoService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(){
        return "transManager/antVideo";
    }

    @RequestMapping(value = "/queryAntVideoInfo",method = RequestMethod.POST)
    @ResponseBody
    public Page queryClaimDemageByMap(HttpSession session, Integer pageIndex, String reportNo,
                                      String relaReportNo,String policyNo,String relaPolicyNo, String state) throws Exception{
        Page page = checkBeforeQuery(session,pageIndex,reportNo,relaReportNo,policyNo,relaPolicyNo,state);
        return  antVideoService.queryAntVideo(page);
    }
    public Page checkBeforeQuery(HttpSession session,Integer pageIndex,String reportNo,
                                 String relaReportNo,String policyNo,String relaPolicyNo,String state) throws Exception{
        Page page = new Page();
        Map<String,Object> map = new HashMap<String, Object>();
        if(pageIndex != -1){
            page.setCurrent(pageIndex);
            map = SqlUtil.buildPageMap(page);
        }
        map.put("state",state);
        map.put("report_no",reportNo);
        map.put("rela_report_no",relaReportNo);
        map.put("policy_no",policyNo);
        map.put("rela_policy_no",relaPolicyNo);
        page.setType(map);
        return page;
    }
    
    @RequestMapping(value = "exportExcel")
	@ResponseBody
	public void exportExcel(HttpServletResponse response, HttpServletRequest request, String reportNo,
            String relaReportNo,String policyNo,String relaPolicyNo, String state) throws Exception {
    	Map<String, String> map = new HashMap<String, String>();
    	map.put("state",state);
        map.put("report_no",reportNo);
        map.put("rela_report_no",relaReportNo);
        map.put("policy_no",policyNo);
        map.put("rela_policy_no",relaPolicyNo);
        //查数据
  		List<Map<String,Object>> policyList = antVideoService.queryAntVideoForExcel(map);
  		String fileName = "蚂蚁保单信息表";
  		String sheetName = "蚂蚁保单信息信息";
  		// 列数
  		int columnNumber = 7;
  		// 列宽
  		int[] columnWidth = {5, 15, 15, 25, 10, 10, 20,  };
  		// 列名
  		String[] columnName = {"序号", "小赔案号", "大赔案号", "小保单号", "大保单号", "影像处理状态",  "交易时间"};
  		// 序号
  		int count = 1;
  		List<String[]> tempList = new ArrayList<String[]>();
  		SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
  		for (Map<String,Object> p : policyList) {
			tempList.add(new String[]{String.valueOf(count++), (String) p.get("REPORT_NO"), (String) p.get("RELA_REPORT_NO"), 
					(String) p.get("POLICY_NO"),(String) p.get("RELA_POLICY_NO"), String.valueOf(p.get("STATE")) ,
					(String)p.get("TRADE_DATE_STRING")});
		}
  		
  		// 海达信息的二维数组【列】【行】
		String[][] haidaInfos = tempList.toArray(new String[][]{});
		HSSFWorkbook wb = antVideoService.generateHssWorkBook(sheetName, columnNumber, columnWidth, columnName, haidaInfos);
		response.setContentType("application/ms-excel;charset=utf-8");
		String userAgent = request.getHeader("user-agent").toLowerCase();
		if (userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge")) {
			// IE浏览器
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} else {
			// 非IE浏览器
			fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
		}
		response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
		OutputStream os = response.getOutputStream();
		try {
			wb.write(os);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			os.close();
		}
    }
}
